
package GUI;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.util.ArrayList;
import java.util.Calendar;

import logic.Member;
import logic.Message;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.Color;
import javax.swing.UIManager;

public class SearchWorker extends JPanel {
	
	private Client_GUI parent;
	private JTextField textFieldStudentName;
	private JTextField textfieldStudentId;
	private JTextField textFieldPhone;
	private JTextField textFieldEmail;
	private JTextField textFieldDateofBirth;
	
	private JLabel LBLDateofBirth;
	private JLabel LBLStudentId;
	private JLabel LBLEmail;
	private JLabel LBLPhone;
	private JLabel LBLstudentname;
	private JLabel lblYouMustPut;
	
	private int Searchaction;
	public JPanel returnPanel;
	
	
	public SearchWorker(final Client_GUI parent,final JPanel returnPanel) {
		this.parent = parent;
		this.setSize(Client_GUI.MAIN_WIDTH, Client_GUI.MAIN_HEIGHT);
		setLayout(null);
		this.setBackground(UIManager.getColor("inactiveCaption"));

		this.returnPanel = returnPanel;
		
		JLabel lblSearchMember = new JLabel("Search Worker");
		lblSearchMember.setForeground(Color.RED);
		lblSearchMember.setFont(new Font("Tahoma", Font.BOLD, 18));
		lblSearchMember.setBounds(325, 25, 157, 38);
		add(lblSearchMember);
		
		JLabel lblStudentName = new JLabel("Worker Name:");
		lblStudentName.setFont(new Font("Dialog", Font.BOLD, 13));
		lblStudentName.setBounds(209, 122, 128, 14);
		add(lblStudentName);
		
		JLabel lblStudentId = new JLabel("Worker Id:");
		lblStudentId.setFont(new Font("Dialog", Font.BOLD, 13));
		lblStudentId.setBounds(209, 172, 128, 14);
		add(lblStudentId);
		
		JLabel lblPhone = new JLabel("Phone:");
		lblPhone.setFont(new Font("Dialog", Font.BOLD, 13));
		lblPhone.setBounds(209, 218, 128, 14);
		add(lblPhone);
		
		JLabel lblEmail = new JLabel("Email:");
		lblEmail.setFont(new Font("Dialog", Font.BOLD, 13));
		lblEmail.setBounds(209, 268, 128, 14);
		add(lblEmail);
		
		JLabel lblDateOfBirth = new JLabel("Date of Birth:");
		lblDateOfBirth.setFont(new Font("Dialog", Font.BOLD, 13));
		lblDateOfBirth.setBounds(209, 315, 128, 14);
		add(lblDateOfBirth);
		
		textFieldStudentName = new JTextField();
		textFieldStudentName.setFont(new Font("Dialog", Font.BOLD, 13));
		textFieldStudentName.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				lblYouMustPut.setVisible(false);
				parent.safeToClear =false ;
			}
		});
		textFieldStudentName.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent arg0) {
				ValidateData(false);
			}
		});
		textFieldStudentName.setBounds(328, 120, 197, 20);
		add(textFieldStudentName);
		textFieldStudentName.setColumns(10);
		
		textfieldStudentId = new JTextField();
		textfieldStudentId.setFont(new Font("Dialog", Font.BOLD, 13));
		textfieldStudentId.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				lblYouMustPut.setVisible(false);
				parent.safeToClear =false ;
			}
		});
		textfieldStudentId.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent arg0) {
				ValidateData(false);
			}
		});
		textfieldStudentId.setColumns(10);
		textfieldStudentId.setBounds(328, 170, 197, 20);
		add(textfieldStudentId);
		
		textFieldPhone = new JTextField();
		textFieldPhone.setFont(new Font("Dialog", Font.BOLD, 13));
		textFieldPhone.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent arg0) {
				ValidateData(false);
			}
		});
		textFieldPhone.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				lblYouMustPut.setVisible(false);
				parent.safeToClear =false ;
			}
		});
		textFieldPhone.setColumns(10);
		textFieldPhone.setBounds(328, 216, 197, 20);
		add(textFieldPhone);
		
		textFieldEmail = new JTextField();
		textFieldEmail.setFont(new Font("Dialog", Font.BOLD, 13));
		textFieldEmail.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent arg0) {
				ValidateData(false);
			}
		});
		textFieldEmail.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				lblYouMustPut.setVisible(false);
				parent.safeToClear =false ;
			}
		});
		textFieldEmail.setColumns(10);
		textFieldEmail.setBounds(328, 266, 197, 20);
		add(textFieldEmail);
		
		textFieldDateofBirth = new JTextField();
		textFieldDateofBirth.setFont(new Font("Dialog", Font.BOLD, 13));
		textFieldDateofBirth.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				lblYouMustPut.setVisible(false);
				parent.safeToClear =false ;
			}
		});
		textFieldDateofBirth.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent arg0) {
				ValidateData(false);
				
			}
		});
		textFieldDateofBirth.setColumns(10);
		textFieldDateofBirth.setBounds(328, 313, 197, 20);
		add(textFieldDateofBirth);
		
		LBLstudentname = new JLabel("");
		LBLstudentname.setBounds(535, 122, 20, 20);
		add(LBLstudentname);
		
		LBLStudentId = new JLabel("");
		LBLStudentId.setBounds(535, 173, 20, 20);
		add(LBLStudentId);
		
		LBLPhone = new JLabel("");
		LBLPhone.setBounds(535, 216, 20, 20);
		add(LBLPhone);
		
		LBLEmail = new JLabel("");
		LBLEmail.setBounds(535, 266, 20, 20);
		add(LBLEmail);
		
		LBLDateofBirth = new JLabel("");
		LBLDateofBirth.setBounds(535, 316, 20, 20);
		add(LBLDateofBirth);
		
		JButton btnSearch = new JButton("Search");
		btnSearch.setFont(new Font("Tahoma", Font.BOLD, 13));
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				parent.safeToClear =false ;
				ValidateData(true);
			}
		});
		btnSearch.setBounds(169, 404, 157, 38);
		add(btnSearch);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.setFont(new Font("Tahoma", Font.BOLD, 13));
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				if (returnPanel == null){
					parent.RepaintMenu(parent.client.Privelege);				
				}else{
					parent.RepaintMain(returnPanel);
				}
				
			}
		});
		btnCancel.setBounds(487, 404, 157, 38);
		add(btnCancel);
		
		lblYouMustPut = new JLabel("You must give at least one input!!!!!!");
		lblYouMustPut.setForeground(Color.RED);
		lblYouMustPut.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblYouMustPut.setBounds(270, 450, 266, 31);
		add(lblYouMustPut);
		lblYouMustPut.setVisible(false);
		
		if (returnPanel != null){
			btnCancel.setText("Back");
		}
	}
	
	
	private void ValidateData(boolean fromOK){

		ImageIcon okImageIcon = new ImageIcon(getClass().getClassLoader().getResource("resource/ok.png"));
		ImageIcon OK = new ImageIcon (okImageIcon.getImage().getScaledInstance(24, 24, java.awt.Image.SCALE_SMOOTH));
		
		ImageIcon badImageIcon = new ImageIcon(getClass().getClassLoader().getResource("resource/bad.png"));
		ImageIcon BAD = new ImageIcon (badImageIcon.getImage().getScaledInstance(24, 24, java.awt.Image.SCALE_SMOOTH));
		
		boolean valid = true;
		
		String temp; // the string to be tested on each test
		/**
		 * validate name
		 */
		if (!textFieldStudentName.getText().equals("")) LBLstudentname.setIcon(OK);
		else LBLstudentname.setIcon(null);
		/**
		 * Validate UID
		 */
		temp = textfieldStudentId.getText();
		if (!temp.equals(""))
		try{
			char c = temp.charAt(0);
			if (c == '+' || c == '-' || temp.length()!=9) {
				LBLStudentId.setIcon(BAD);
				valid = false;
			}
			else LBLStudentId.setIcon(OK);	
			Integer.parseInt(temp);	
		}catch(NumberFormatException e){
			LBLStudentId.setIcon(BAD);
			valid = false;
		}
		else LBLStudentId.setIcon(null);
		
		
		/**
		 * verifying birth date
		 */
		temp =textFieldDateofBirth.getText();
		if(temp.length()== 10)
			try
			{
				if ((temp.charAt(4) == '-' && temp.charAt(7) == '-') || (temp.charAt(4) == '/' && temp.charAt(7) == '/') || (temp.charAt(4) == '.' && temp.charAt(7) == '.')) 
					LBLDateofBirth.setIcon(OK);
				else {
					LBLDateofBirth.setIcon(BAD);
					valid = false;
				}
				
				int year = Integer.parseInt(temp.substring(0, 4));
				int month = Integer.parseInt(temp.substring(5, 7));
				int day = Integer.parseInt(temp.substring(8, temp.length()));

				Calendar validator = Calendar.getInstance();
				validator.set(year, month-1, day);
				validator.setLenient(false);
				validator.getTime();
				
			}catch(NumberFormatException e){
				LBLDateofBirth.setIcon(BAD);
				valid = false;
			}catch(Exception e){
				LBLDateofBirth.setIcon(BAD);
				valid = false;
			}
			else if(!temp.equals("")) {
				LBLDateofBirth.setIcon(BAD);
				valid = false;
			}
			else LBLDateofBirth.setIcon(null);
		
		
		//validate phone
		if (!textFieldPhone.getText().equals("")){
			String phone = textFieldPhone.getText();
			phone = phone.replace("-", "");
			phone = phone.replace(" ", "");
			try{
				Integer.parseInt(phone);
				LBLPhone.setIcon(OK);
			}catch(NumberFormatException e){
				LBLPhone.setIcon(BAD);
				valid = false;
			}
			if(textFieldPhone.getText().length()!=9 && textFieldPhone.getText().length()!=10
							&& textFieldPhone.getText().length()!=11)
			{
				LBLPhone.setIcon(BAD);
				valid = false;
			}
		}
		else LBLPhone.setIcon(null);
		
		/**
		 * validate Email
		 */
		if (!textFieldEmail.getText().equals("")){
			int schtroodelLocation = textFieldEmail.getText().indexOf("@");
			
			if (textFieldEmail.getText().indexOf("@", schtroodelLocation) == -1){//another @ exists
				LBLEmail.setIcon(BAD);
				valid = false;
			}else if (textFieldEmail.getText().indexOf(".", schtroodelLocation) == -1){
				LBLEmail.setIcon(BAD);
				valid = false;
			}else if (schtroodelLocation == 0){
				LBLEmail.setIcon(BAD);
				valid = false;
			}else LBLEmail.setIcon(OK);
		}
		else LBLEmail.setIcon(null);
		
		
		
		
		if (fromOK){
			if(textfieldStudentId.getText().equals("") && textFieldStudentName.getText().equals("") && textFieldDateofBirth.getText().equals("") && textFieldEmail.getText().equals("") && textFieldPhone.getText().equals(""))
			{
		    valid = false;
			lblYouMustPut.setVisible(true);
			
			}
			
		}
		
		
		if (valid && fromOK){
			ArrayList<String> index = new ArrayList<String>();
			ArrayList<Object> data = new ArrayList<Object>();
			
			index.add("DIRECTOR");
			data.add(this.Searchaction);
			
			if(!textfieldStudentId.getText().equals(""))
			{
			index.add("UID");
			data.add(textfieldStudentId.getText());
			}
			if(!textFieldStudentName.getText().equals(""))
			{
			index.add("NAME");
			data.add(textFieldStudentName.getText());
			}
			if(!textFieldDateofBirth.getText().equals(""))
			{
			index.add("DOB");
			data.add(textFieldDateofBirth.getText());
			}
			if(!textFieldEmail.getText().equals(""))
			{
			index.add("MAIL");
			data.add(textFieldEmail.getText());
			}
			if(!textFieldPhone.getText().equals(""))
			{
			index.add("PHONE");
			data.add(textFieldPhone.getText());
			}

			
			int action = Message.SEARCH_WORKER_REQUEST;

			
			Message toSend = new Message(index, data, action);
			
			if (parent.sendMessageToServer(toSend) == 1){//message sent successfully
				parent.SetLoading(true);
				
			}else{//message send failed
				//TODO verify extream situation
			}
			
		}
	}
	
}
